package com.yang.config;

import com.yang.imbatis.scan.YangMapperScan;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @Description: spring的核心配置类
 * @author: Yang Qiang
 * @create: 2021-10-18 21:26
 */
@Configuration
@ComponentScan(value = "com.yang")
@YangMapperScan
//@EnableAspectJAutoProxy
//@ImportResource("classpath:spring-mybatis.xml")
public class SpringConfig {
	/**
	 * 数据源
	 */
	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
		driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		driverManagerDataSource.setUsername("root");
		driverManagerDataSource.setPassword("root");
		driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8");
//		driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&allowPu");
		return driverManagerDataSource;
	}

	/**
	 *  mybatis连接工厂
	 */
	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource());
		return factoryBean.getObject();
	}
}
